const { contextBridge, ipcRenderer } = require("electron");

console.log("Preload script is running..."); // 添加调试日志

// 暴露API给渲染进程
contextBridge.exposeInMainWorld("primaryWindowAPI", {
  // 现有API
  getAutoLoginCookie: () => {
    console.log("getAutoLoginCookie called from preload"); // 添加调试日志
    return ipcRenderer.invoke("get-auto-login-cookie");
  },
  updateCookie: (cookie) => ipcRenderer.invoke("update-cookie"),
  submitTrafficData: (data) => ipcRenderer.invoke("submit-traffic-data", data),
  createWorkOrder: (data) => ipcRenderer.invoke("create-work-order", data),
  readExcelFile: (filePath) => ipcRenderer.invoke("read-excel-file", filePath),
  uploadImage: (options) => {
    console.log("preload: 调用uploadImage");
    return ipcRenderer.invoke("upload-image", options);
  },

  // 新增API
  uploadFile: () => ipcRenderer.invoke("upload-file"),
  uploadWordDocument: () => ipcRenderer.invoke("upload-word-document"),

  // 添加一个简单的测试对话框API
  testSimpleDialog: () => {
    console.log("preload: 调用testSimpleDialog");
    return ipcRenderer.invoke("test-simple-dialog");
  },

  // 添加上传文件的API
  uploadFileWithBase64: (data) =>
    ipcRenderer.invoke("upload-file-with-base64", data),

  // 添加上传文件的API
  uploadFileWithForm: (data) =>
    ipcRenderer.invoke("upload-file-with-form", data),

  // 添加上传PDF文件的API
  uploadPdfFile: (data) => ipcRenderer.invoke("upload-pdf-file", data),
});

// 其他electron API
contextBridge.exposeInMainWorld("electron", {
  openDevTools: () => ipcRenderer.send("open-dev-tools"),
  logout: () => ipcRenderer.send("logout"),
  saveTempFile: (data, fileName) =>
    ipcRenderer.invoke("save-temp-file", data, fileName),
  deleteTempFile: (filePath) =>
    ipcRenderer.invoke("delete-temp-file", filePath),
});

console.log("Preload script finished loading"); // 添加调试日志
